<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp" %>

<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="message.jieSuan.woDeJieSuanDan"/> <spring:message code="message.listForm.weiShengJu"/></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp" %>
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
							
    <section class="content-header">
        <h1><spring:message code="message.jieSuan.woDeJieSuanDan"/></h1>
         <ol class="breadcrumb">
            <li><a href="${ctx }/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.jieSuan.guanLi"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.listForm.weiShengJu"/><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.jieSuan.guanLi"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.jieSuan.woDeJieSuanDan"/></a></li>
        </ol>
    </section>
   
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
                <div class="form-inline">
                	<div class="form-group form-group-xs">
                       <spring:message code="message.jieSuan.chuangJianRiQi"/><spring:message code="message.yanZheng.maoHao"/> 
                       <input class="form-control" type="text" id="beginDate"  name="beginDate" placeholder="yyyy-MM-dd"  onFocus="var endDate=$dp.$('endDate');WdatePicker({maxDate:'#F{$dp.$D(\'endDate\')}'})" />
                        -
                       <input class="form-control" type="text" id="endDate"  name="endDate" placeholder="yyyy-MM-dd"  onFocus="WdatePicker({minDate:'#F{$dp.$D(\'beginDate\')}'})" />
                   </div>
			        <button id="selectWSJ"  type="button" class="btn btn-success btn-sm" onclick="gridSerch();"><spring:message code="message.button.seachSpacing"/></button>
			       </div>
                   
                </div>
            </div>
        
        <table class="jqgrid" id="gridlist"></table>
		<div id="gridpage"></div>
		<div class=" box box-warning bottongroup">
			<label id="addNew"  class="btn btn-default btn-sm"><spring:message code="message.button.addSpacing"/></label>
            <button type="button" onclick="removeDel();" class="btn btn-default btn-sm"><spring:message code="message.jieSuan.shanChuSpacing"/></button>
            <button type="button" onclick="batchJoin(0)" class="btn btn-default btn-sm"><spring:message code="message.button.submitSpacing"/></button>

		</div>
		
	<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header bg-primary">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h5 class="modal-title" id="exampleModalLabel"><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.button.add"/></h5>
				</div>
				<div class="modal-body">
					<form>
						<div class="form-group form-group-xs">
							<label for="recipient-name" class=" control-label" id="operflag"><span class="required">*</span><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/><spring:message code="message.yanZheng.maoHao"/></label>
							<div class="">
								<input type="hidden" name="settlementId" id="settlementId" value="">
								<input type="text" class="form-control" id="textsettlementName" placeholder="<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/>">
							</div>
						</div>
						<div class="form-group form-group-xs">
							<label for="recipient-name" class=" control-label"><spring:message code="message.lable.remark"/><spring:message code="message.yanZheng.maoHao"/></label>
							<div class="">
								<input type="text" class="form-control" id="remarks" placeholder="<spring:message code="message.lable.remark"/>">
							</div>
						</div>

					</form>
				</div>
				<div class="modal-footer">
                    <button type="button" onclick="save();" class="btn btn-primary btn-sm"><spring:message code="message.button.saveSpacing"/></button>
                    <button type="button" id="closebtn" class="btn btn-danger btn-sm" data-dismiss="modal"><spring:message code="message.button.closeSpacing"/></button>
                </div>
			</div>
		</div>
	</div>
	<div class="modal fade" id="exampleModaltext" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header bg-primary">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h5 class="modal-title" id="exampleModalLabel"><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.button.edit"/></h5>
				</div>
				<div class="modal-body">
					<form>
						<div class="form-group form-group-xs">
							<label for="recipient-name" class=" control-label" id="operflagtext"><span class="required">*</span><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/><spring:message code="message.yanZheng.maoHao"/></label>
							<div class="">
								<input type="hidden" name="settlementId" id="settlementId" value="">
								<input type="text" class="form-control" id="testsettlementName" placeholder="<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/>">
							</div>
						</div>
						<div class="form-group form-group-xs">
							<label for="recipient-name" class=" control-label"><spring:message code="message.lable.remark"/><spring:message code="message.yanZheng.maoHao"/></label>
							<div class="">
								<input type="text" class="form-control" id="testremarks" placeholder="<spring:message code="message.lable.remark"/>">
							</div>
						</div>

					</form>
				</div>
				<div class="modal-footer">
                    <button type="button" onclick="saveTwo();" class="btn btn-primary btn-sm"><spring:message code="message.button.saveSpacing"/></button>
                    <button type="button" id="closebtn" class="btn btn-danger btn-sm" data-dismiss="modal"><spring:message code="message.button.closeSpacing"/></button>
                </div>
			</div>
		</div>
	</div>
    </section>
    
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
    <script type="text/javascript">
    
  //加载页面光标在名字框内，位于第一个
  //  $("#heaBurName").focus();
    
  //绑定回车    id="selectWSJ"  
    $('#beginDate,#endDate').bind('keydown', function(event) {
        if (event.keyCode == "13") {        //13 位电脑上回车键的位置代码
            $('#selectWSJ').click();
        }
    });
    
        $(function () {
        	
        	
        	jQuery("#gridlist").jqGrid({
    			url: "${ctx}/settlementFirst/getSettlementSecondDataList.html",
    		    mtype: "POST",
    			datatype: "json",
    			autowidth: true,
    			shrinkToFit: false,
    			height: 270,
    		   	colNames:[
								'<spring:message code="message.jqGrid.cz"/>',
								'<spring:message code="message.jieSuan.jieSuanDan"/>id',
								'<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.yanZheng.bianMa"/>',
								'<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/>',
								'<spring:message code="message.jieSuan.zongTiaoMuShu"/>',
								'<spring:message code="message.jieSuan.zongJinE"/>',
								'<spring:message code="message.jieSuan.chuangJianShiJian"/>',
								'<spring:message code="message.jieSuan.chuangJianRen"/>',
								'<spring:message code="message.lable.remark"/>',
							    '<spring:message code="message.lable.status"/>'
    		   	          
    		   	          ],
    		   	colModel:[
					{ name: 'oper', width:160, align:"center", sortable:false,
					    formatter : function(val, opts, rowdata) {
					        var str = "";
					        str += "<a href=\"javascript:edit('"+ rowdata.settlementId+ "');\"><spring:message code="message.button.edit"/></a>"
					             + "<a href='${ctx}/settlementFirst/toAddSecondDetailList.html?settlementId="+rowdata.settlementId+"'><spring:message code="message.button.addDetail"/></a>"
					             + "<a href='${ctx}/settlementFirst/toSetSettlementDetail.html?settlementId="+rowdata.settlementId+"'><spring:message code="message.jqGrid.cz.xiangQing"/></a>";
					        return str;
					    }  
					},
    				{ name: 'settlementId',width:160,align:"center" ,hidden:true, sortable:false},
    		   		{ name: 'settlementCode', width:160, align:"center",sortable:false},
    		   		{ name: 'settlementName', width:160, align:"left",sortable:false},
    		   		{ name: 'totalOrders', width:80,align:"center",sortable:false},
    		   		{ name: 'totalAmount', width:100,align:"right",sortable:false,
						formatter :function(val){
							return val.toFixed(2);
						}},
    		   		{ name: 'createDatetime', width:160, align:"center",sortable:false},
    		   		{ name: 'createUser', width:160, align:"left", sortable:false},
    		   		{ name: 'remarks', width:160, align:"left", sortable:false},
    		   		{ name: 'status',hidden:true, width:80, align:"center", sortable:false}
    		   		
    		   	],
    			 rowNum: 20,
    		     rowList: [10, 20, 50, 100],
    		     rownumbers: true,
    		     pager: "#gridpage",
    		     viewrecords: true,
    		     multiselect: true, 
    		     caption: "<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.lieBiao"/>",
    		     //序号宽度自动变化
                 gridComplete:function(){
                     //序号列宽度自适应（参数为jqgridID）
                     autoRNWidth("gridlist");
                     //数据只有一条时默认选中（参数为jqgridID）
                     selectByOneData("gridlist");
                 },
    		     jsonReader: {
    		         repeatitems: false,
    		         id: "ids"
    		     }
    		});
            
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            
            $(window).trigger("resize");
            
        });
        
        // 检测名称是否相同
        function checkSameName(settlementId, settlementName) {
            var exists = false;
            $.ajax({
                type: "post",
                url: "checkSameName.html",
                async : false,
                data: {
                    settlementId : settlementId || "",
                    settlementName : settlementName || ""
                },
                success: function(data, textStatus){
                    var json = eval('(' + data + ')');
                    exists = json.success;
                }
            });
            return exists;
        }
        
      //查询
        function gridSerch() {
        	var beginDate = $.trim($("#beginDate").val());
        	var endDate = $.trim($("#endDate").val());
        	var submitBeginDate = $.trim($("#submitBeginDate").val());
        	var submitEndDate = $.trim($("#submitEndDate").val());
        	var status = $.trim($("#status").val());
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{
    	        		"beginDate":beginDate,
    	        		"endDate":endDate,
    	        		"submitBeginDate":submitBeginDate,
    	        		"submitEndDate":submitEndDate,
    	        		"status":status},
    	        		page:1
    	    }).trigger("reloadGrid"); //重新载入  
    	}
      
      //新曾
        $('#addNew').click(
    			function() {
    				$('.modal-title').html('<spring:message code="message.button.add"/><spring:message code="message.jieSuan.jieSuanDan"/>');
    				$("#operflag").val("add");
    				$('#textsettlementName').val("");
    				$('#remarks').val("");
    				$('#exampleModal').modal({
    					keyboard : true
    				});
    			});
      //保存
		function save() {
				var settlementName = $.trim($('#textsettlementName').val());
				if (settlementName.length < 1){
					$.HN.message.alert('<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>','<spring:message code="message.HN.alert.tiShiXinXi"/>','warn');
	                return;
				}
				var remarks = $.trim($('#remarks').val());
				var operflag = $.trim($("#operflag").val());
				if (checkSameName(null, settlementName)) {
					$.HN.message.alert('<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/><spring:message code="message.lable.yiChunZai"/>','<spring:message code="message.HN.alert.tiShiXinXi"/>','warn');
	                return;
	            }
				$.HN.message.confirm('<spring:message code="message.HN.alert.OkSaveYESorNO"/>', '', '').on(function(e) {
					if (e) {
						$.ajax({
							url : "${ctx}/settlementFirst/saveSettlementSecond.html",//action地址
							data : {
								settlementName : settlementName,
								remarks : remarks,
								operflag : operflag,
							},//传值
							type : "post",
							success : function(result) {
								var result = eval("("+ result+ ")");
								if ("success" == result) {
									$.HN.message.alert('<spring:message code="message.HN.alert.fail"/>','<spring:message code="message.listForm.xinXi"/>','success');
									gridSerch()
									$("#closebtn").click();
								}  else {
									$.HN.message.alert('<spring:message code="message.HN.alert.success"/>','<spring:message code="message.listForm.xinXi"/>','success');
									gridSerch()
									$("#closebtn").click();
								}
							
							} 
						});
					}
				});
			}
      	
      
		var globalSettlementId = null;
      //编辑
      function edit(settlementId) {
		$("#operflagtext").val("edit");
		$('.modal-title').html('<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.button.edit"/>');
		globalSettlementId = settlementId;
		$.ajax({
			url : "${ctx}/settlementFirst/toEditSettlementSecond.html",//action地址s
			data : {settlementId : settlementId},//传值
			type : "post",//执行方式get or post 默认以post方式执行
			datatype : "json",
			success : function(data) {//执行成功后 提示
				var data = eval("(" + data + ")");
				$('#testsettlementName').val(data.settlementName);
				$('#testremarks').val(data.remarks);
				$('#exampleModaltext').modal({
					keyboard : true
				});
			},
			error : function() {// 如果执行出现错误 提示
				alert("<spring:message code="message.jieSuan.chuCuoChongShi"/>");
			}
		});
	}
    //编辑保存
		function saveTwo() {
			
				var settlementName = $.trim($('#testsettlementName').val());
				if (settlementName.length < 1){
					$.HN.message.alert('<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>','<spring:message code="message.HN.alert.tiShiXinXi"/>','warn');
	                return;
				}
				var remarks = $.trim($('#testremarks').val());
				var operflag = $.trim($("#operflagtext").val());
				if (checkSameName(globalSettlementId, settlementName)) {
					$.HN.message.alert('<spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.listForm.mingCheng"/><spring:message code="message.lable.yiChunZai"/>','<spring:message code="message.HN.alert.tiShiXinXi"/>','warn');
	                return;
	            }
				$.HN.message.confirm('<spring:message code="message.HN.alert.OkSaveYESorNO"/>', '', '').on(function(e) {
						if (e) {
					$.ajax({
						url : "${ctx}/settlementFirst/editSettlementSecond.html",//action地址
						data : {
							settlementId : globalSettlementId,
							settlementName : settlementName,
							remarks : remarks,
							operflag : operflag
						},//传值
						type : "post",
						success : function(result) {
							var result = eval("("+ result+ ")");
							if ("success" == result) {
								$.HN.message.alert('<spring:message code="message.HN.alert.fail"/>','<spring:message code="message.listForm.xinXi"/>','success');
								gridSerch()
								$("#closebtn").click();
							}  else {
								$.HN.message.alert('<spring:message code="message.HN.alert.success"/>','<spring:message code="message.listForm.xinXi"/>','success');
								gridSerch()
								$("#exampleModaltext").modal("hide");
	
							}
						
						} 
					});
				}
			});
		}
      
    //删除
      function removeDel(){
		    var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
			if (rowIds.length <= 0) {
				$.HN.message.alert("<spring:message code="message.jieSuan.qingXuanZheShanChu"/><spring:message code="message.jieSuan.jieSuanDan"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return;
			}
			$.HN.message.confirm('<spring:message code="message.jieSuan.queDingShanChuMa"/>', '', '').on(function (e) {
				if (e) {
					var list = [];
					for (var i=0; i<rowIds.length; i++) {
						rowId = rowIds[i];
						var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
						list.push({"settlementId":rowdata.settlementId});
					}
					$.post("${ctx}/settlementFirst/deleteSettlementSecond.html", {
						"array" : json.jsonToString(list)
					}, function(result) {
						gridSerch();
						if (result.success) {
							$.HN.message.alert("<spring:message code="message.HN.alert.success"/>", "<spring:message code="message.HN.alert.type"/>", "success");
						} else {
							$.HN.message.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "<spring:message code="message.HN.alert.type"/>",  "error");
						}
					}, "json");
				}
			});
		}
      //提交
      function batchJoin(code){
    	 
        	var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    		if (rowIds.length <= 0) {
    			$.HN.message.alert("<spring:message code="message.HN.alert.pleaseSelectLine"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return;
    		}
    		var list = [];
            for (var i=0; i<rowIds.length; i++) {
                rowId = rowIds[i];
                var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
                if (rowdata.totalOrders == 0) {
                	$.HN.message.alert("<spring:message code="message.jieSuan.qingXianTianJia"/><spring:message code="message.jieSuan.jieSuanDanMingXi"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
                }
                list.push({"settlementId":rowdata.settlementId}); 
            }
    		$.HN.message.confirm('<spring:message code="message.jieSuan.queDingTiJiaoMa"/>', '', '').on(function (e) {
    			if (e) {
    	    		$.post("${ctx}/settlementFirst/subSettlementSecond.html", {
    	    			"array" : json.jsonToString(list),"code":code
    	    		}, function(result) {
    	    			if (result.success) {
    	    				gridSerch();
    	    				$.HN.message.alert("<spring:message code="message.HN.alert.success"/>", "<spring:message code="message.HN.alert.type"/>", "success");
    	    			} else {
    	    				if(result.code ==1){
    	    					batchJoin(1);
    	    				}
    	    				$.HN.message.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "<spring:message code="message.HN.alert.type"/>", "error");
    	    			}
    	    		}, "json")
    			}
    		});
        }
      
    </script>
</body>
</html>












